package oschina.by.isrefluxer.com.weatherapplication;

import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.ContentObserver;
import android.net.Uri;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;

import xml.NewAppWidget;


/**
 * 项目名称：WeatherApplication
 * 类描述：
 * 创建人：Administrator
 * 创建时间：2016/10/18 15:27
 * 修改人：Administrator
 * 修改时间：2016/10/18 15:27
 * 修改备注：
 */
public class WeatherServer extends Service {

    private static String  uri = "content://com.yunos.weather.provider/location";
    private ContentResolver resolver;
    private static String TAG = "WeatherServer";
    private Context context;
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.e(TAG,"onCreate");
        context = getApplicationContext();
        resolver = getContentResolver();
        resolver.registerContentObserver(Uri.parse(uri),true,new WeathProvider(new Handler()));
        BitmapsUtil.startTemperBitmaps(context);
    }

    private class WeathProvider extends ContentObserver {
        public WeathProvider(Handler handler) {
            super(handler);
        }

        @Override
        public void onChange(boolean selfChange) {
            super.onChange(selfChange);
            AppWidgetManager awm =AppWidgetManager.getInstance(context);
            int []appWidgetIds = awm.getAppWidgetIds(new ComponentName(context, NewAppWidget.class));
            NewAppWidget.updateWeatherWidget(context,awm,appWidgetIds);
        }
    }

}
